package com.qinglei.recoup.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.service.RedisService;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.*;
import com.qinglei.recoup.system.pojo.AddBedParam;
import com.qinglei.recoup.system.pojo.TimePeriodParam;
import com.qinglei.recoup.system.pojo.UpdateOutBedTimeoutParam;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.BuildingService;
import com.qinglei.recoup.system.service.MonitorTimeService;
import com.qinglei.recoup.system.service.VitalSignsAlarmService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * @author dqh
 */
@RestController
@RequestMapping("/vital-signs-alarm")
public class VitalSignsAlarmController {


    @Resource
    private VitalSignsAlarmService  vitalSignsAlarmService;

    @Resource
    private RedisService redisService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private MonitorTimeService monitorTimeService;

    @PostMapping("/select")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes selectVitalSignsAlarm(@RequestBody Map<String, Object> map) throws Exception {
        //查询
        List<VitalSignsAlarm> list = vitalSignsAlarmService.list(Wrappers.<VitalSignsAlarm>lambdaQuery().eq(VitalSignsAlarm::getTenantId, map.get("tenantId")));
        return CommonRes.ok(list);
    }
    @PostMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public CommonRes updateVitalSignsAlarm(@RequestBody List<VitalSignsAlarm> vitalSignsAlarmList) throws Exception {
        //修改
        vitalSignsAlarmService.updateBatchById(vitalSignsAlarmList);
        vitalSignsAlarmList.forEach(t->{
            if(t.getSignType()==1 && t.getEnable()==1){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("tenant_id",t.getTenantId());
                List<Building> buildings =   buildingService.getBaseMapper().selectList(queryWrapper);
                buildings.forEach(s->{
                    UpdateOutBedTimeoutParam param1 = new UpdateOutBedTimeoutParam();
                    param1.setObjId("b"+s.getId());
                    param1.setType(4);
                    param1.setOutBedTimeout(LocalTime.of(0,0, Integer.parseInt(t.getContinued())).toString());
                    monitorTimeService.updateOutBedTimeout(param1);
                });
            }
        });
        List<VitalSignsAlarm> VitalSignsAlarmList2 = vitalSignsAlarmService.list(Wrappers.<VitalSignsAlarm>lambdaQuery().eq(VitalSignsAlarm::getTenantId, vitalSignsAlarmList.get(0).getTenantId()));
        VitalSignsAlarmList2.forEach(f->{
            f.setCreateTime(null);
            f.setModifyTime(null);
            String s = JsonUtils.objectToJson(f);
            try {
                redisService.set("VitalSignsAlarm-"+f.getTenantId()+"-"+f.getSignType(),s);
            } catch (RedisConnectException e) {
                e.printStackTrace();
            }

        });
        return CommonRes.ok("修改成功");
    }
}
